import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useState, useCallback, useReducer, useMemo } from 'react';
import produce from 'immer';
/**
 * 引用对象不被修改，引用对象每被修改则返回一个新的对象
 * @param initialValue
 */
export function useImmer(initialValue) {
  var _useState = useState(initialValue),
    _useState2 = _slicedToArray(_useState, 2),
    val = _useState2[0],
    setVal = _useState2[1];
  return [val, useCallback(function (updater) {
    if (typeof updater === 'function') {
      return setVal(produce(updater));
    }
    return setVal(updater);
  }, [])];
}
export function useImmerReducer(reducer, initialState, initialAction) {
  var cachedReducer = useMemo(function () {
    return produce(reducer);
  }, [reducer]);
  return useReducer(cachedReducer, initialState,
  // @ts-ignore
  initialAction);
}